package com.ErdosWebService.ModelService;

import com.weavernorth.modelInterFace.pojo.detailVo;
import com.weavernorth.modelInterFace.pojo.filedEntity;
import com.weavernorth.modelInterFace.pojo.mainVo;
import com.weavernorth.modelInterFace.pojo.requestDataVo;
import com.weavernorth.modelInterFace.service.modeDaoImpl;
import com.weavernorth.util.Logger4jUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.jws.WebService;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: bruseewang
 * @Package: com.ErdosWebService.ModelService.webservice
 * @date: 2020/8/20 16:23
 * @description: 外部系统操作OA建模表通用接口（WebService）
 */
@WebService(endpointInterface = "com.ErdosWebService.ModelService.MsgService")
public class ModelServiceImpl implements MsgService {
    private Lock lock = new ReentrantLock();
    private Logger log = Logger4jUtil.getLoggerByName("/action/api/OperationData/");
    public String getValue(requestDataVo vo){
        log.info("webService统一建模接口调用开始：");
        //返回json
        JSONObject jsonReturn = new JSONObject();
        log.info("接口传入的数据："+vo.toString());
        Boolean flag = true;
        lock.lock();//获取锁,如果锁被暂用则一直等待
        try {
            String strSysName = vo.getSysName();//获取外围系统名称
            String strSysTable = vo.getSysTable();//获取表名
            String struserName = vo.getUserName();//获取用户名
            String strpassWorld = vo.getPassWord();//获取密码
            String strmainKey = vo.getMainKey();//获取主键
            String isDel = vo.getIsDel();//获取是否删除
            List<mainVo> main_list = vo.getMain_list();//获取主表集合
            log.info("main_List对象:" + main_list);
//        for (mainVo mainVo : main_list) {
//            String tbKey = mainVo.getTbKey();//获取主键
//            List<filedEntity> data = mainVo.getData();//获取列名及列的值
//            for (filedEntity datum : data) {
//                String filedName = datum.getFiledName();//获取列名
//                String filedValue = datum.getFiledValue();//获取列的值
//            }
//        }
            List<detailVo> detail_list = vo.getDetail_list();//获取明细表集合
            log.info("detail_list明细表对象" + detail_list);
//        for (detailVo detailVo : detail_list) {
//            String detailNo = detailVo.getDetailNo();//明细表行项目
//            String tbKey = detailVo.getTbKey();//主键
//            List<filedEntity> data = detailVo.getData();//获取列名及列的值
//            for (filedEntity datum : data) {
//                String filedName = datum.getFiledName();//获取列名
//                String filedValue = datum.getFiledValue();//获取列的值
//            }
//        }

            int list_Count = 0;

            //获取主表表对象集合
            //2.查询权限表:uf_jmqx
            String strSql = "select * from uf_jmqx where wwxt='" + strSysName + "' and wwbm ='" + strSysTable + "' and yhm='" + struserName + "' and mm='" + strpassWorld + "'";
            RecordSet rs = new RecordSet();
            rs.executeQuery(strSql);
            Integer rowsALl = rs.getCounts();
            String strOaTable = "";
            if (rowsALl > 0) {
                while (rs.next()) {
                    //3.权限通过,查找要更新的OA系统的表
                    strOaTable = Util.null2String(rs.getString("oabm"));
                }
                if (!"".equals(strOaTable)) {
                    list_Count = main_list.size();
                    log.info("数据总数：" + list_Count);
                    if (list_Count > 0) {
                        if (!main_list.equals("Y")) {
                            //操作主表
                            if (main_list.size() > 0) {
                                log.info("对主表操作开始。。。");
                                for (mainVo mainVo : main_list) {
                                    //只要有一个错误就不再继续
                                    if (flag) {
                                        //主键
                                        String strTbkey = mainVo.getTbKey();
                                        List<filedEntity> data = mainVo.getData();
                                        int dataSize = data.size();
                                        if (dataSize > 0) {
                                            log.info("main的集合数据：" + data.toString());
                                            String strMainSql = "select * from " + strOaTable + " where mainKey='" + strTbkey + "'";
                                            log.info("查询主表sql:" + strMainSql);

                                            ConnStatement rs_main_tb = new ConnStatement();
                                            rs_main_tb.setStatementSql(strMainSql);
                                            rs_main_tb.executeQuery();
                                            if (rs_main_tb.next()) {
                                                log.info("更新数据开始：");
                                                modeDaoImpl dao = new modeDaoImpl();
                                                //调用更新数据方法
                                                String update_rs = "";//dao.updateData(strOaTable, data, strTbkey, rs_main_tb);
                                                log.info("更新主表数据返回结果：" + update_rs);
                                                String code = update_rs.substring(0, update_rs.indexOf("|"));
                                                String msg = update_rs.substring(update_rs.indexOf("|") + 1, update_rs.length());
                                                if (!"0".equals(code)) {
                                                    jsonReturn.put("status", "F");
                                                    jsonReturn.put("message", msg);
                                                    flag = false;
                                                    break;
                                                }

                                            } else {
                                                //不存在则新增
                                                log.info("插入数据开始：");
                                                modeDaoImpl dao = new modeDaoImpl();
                                                String insert_rs = "";//dao.insertData(strOaTable, data, strTbkey);
                                                log.info("插入主表数据返回结果：" + insert_rs);
                                                String code_ins = insert_rs.substring(0, insert_rs.indexOf("|"));
                                                String msg_ins = insert_rs.substring(insert_rs.indexOf("|") + 1, insert_rs.length());
                                                if (!"0".equals(code_ins)) {
                                                    jsonReturn.put("status", "F");
                                                    jsonReturn.put("message", msg_ins);
                                                    flag = false;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                log.info("对主表操作结束。。。。");
                            }
                            if (!"".equals(strmainKey)) {//操作明细表
                                if (detail_list.size() > 0) {
                                    log.info("对明细表操作开始。。。。");
                                    for (detailVo detailVo : detail_list) {
                                        if (flag) {
                                            //明细表编号(第几个明细表)
                                            String strDetailNo = detailVo.getDetailNo();
                                            //明细表表名
                                            String strTb_detail = strOaTable + "_dt" + strDetailNo;
                                            //明细表主键
                                            String strTbkey = detailVo.getTbKey();
                                            List<filedEntity> data_detail = detailVo.getData();
                                            int dataSize = data_detail.size();
                                            if (dataSize > 0) {
                                                log.info("detail的集合数据：" + data_detail.toString());
                                                String strDetailSql = "select * from " + strTb_detail + " where mainKey='" + strmainKey + "' and tbKey='" + strTbkey + "'";
                                                log.info("查询明细表sql:" + strDetailSql);
                                                try {
                                                    ConnStatement rs_detail_tb = new ConnStatement();
                                                    rs_detail_tb.setStatementSql(strDetailSql);
                                                    rs_detail_tb.executeQuery();
                                                    if (rs_detail_tb.next()) {
                                                        log.info("更新数据开始：");
                                                        modeDaoImpl dao = new modeDaoImpl();
                                                        //调用更新数据方法
                                                        String update_rs = "";//dao.update_detail(strTb_detail, data_detail, strTbkey, strmainKey, rs_detail_tb);
                                                        log.info("更新明细表数据返回结果：" + update_rs);
                                                        String code_up = update_rs.substring(0, update_rs.indexOf("|"));
                                                        String msg_up = update_rs.substring(update_rs.indexOf("|") + 1, update_rs.length());
                                                        if (!"0".equals(code_up)) {
                                                            jsonReturn.put("status", "F");
                                                            jsonReturn.put("message", msg_up);
                                                            flag = false;
                                                            break;
                                                        }
                                                    } else {
                                                        //不存在则新增
                                                        log.info("插入数据开始：");
                                                        modeDaoImpl dao = new modeDaoImpl();
                                                        String insert_rs ="";// dao.insert_detail(strTb_detail, data_detail, strTbkey, strmainKey);
                                                        log.info("插入明细表数据返回结果：" + insert_rs);
                                                        String code = insert_rs.substring(0, insert_rs.indexOf("|"));
                                                        String msg = insert_rs.substring(insert_rs.indexOf("|") + 1, insert_rs.length());
                                                        if (!"0".equals(code)) {
                                                            jsonReturn.put("status", "F");
                                                            jsonReturn.put("message", msg);
                                                            flag = false;
                                                            break;
                                                        }
                                                    }
                                                } catch (Exception ex) {
                                                    log.info("查询数据出现异常：" + ex.toString());
                                                    jsonReturn.put("status", "F");
                                                    jsonReturn.put("message", "出现异常：" + ex.toString());
                                                    flag = false;
                                                    break;
                                                }
                                            }

                                        }
                                    }
                                    log.info("对明细表操作结束。。。。");
                                }
                            } else {
                                flag = false;
                                log.info("接口detail_list为空！");
                                jsonReturn.put("status", "F");
                                jsonReturn.put("message", "接口传输数据为空!");
                            }
                        } else {
                            log.info("删除操作");
                            String strTbKey = "";//主键
                            String strTbName = "";//表名
                            String strKey_m = "";//更新明细表时主表主键
                            if (main_list.size() > 0) {
                                for (mainVo mainVo : main_list) {
                                    String strMain = mainVo.getTbKey();
                                    //执行删除操作
                                    modeDaoImpl dao = new modeDaoImpl();
                                    log.info("要删除的主表表名：" + strTbName + ";要删除的数据主键：" + strMain);
                                    String strDelResult = dao.delete_Data(strTbName, strMain, strmainKey);
                                    String strCode = strDelResult.substring(0, strDelResult.indexOf("|"));
                                    String msg_up = strDelResult.substring(strDelResult.indexOf("|") + 1, strDelResult.length());
                                    if (!"0".equals(strCode)) {
                                        flag = false;
                                        jsonReturn.put("status", "F");
                                        jsonReturn.put("message", msg_up);
                                        break;
                                    }
                                }
                            }
                            if (detail_list.size() > 0 && !"".equals(strmainKey)) {
                                strKey_m = strmainKey;
                                //删除明细表数据
                                for (detailVo detailVo : detail_list) {
                                    //主键
                                    strTbKey = detailVo.getTbKey();
                                    strTbName = strOaTable + "_dt" + detailVo.getDetailNo();//获取明细表表名
                                    if (!"".equals(strTbKey) && !"".equals(strTbName)) {
                                        //执行删除操作
                                        modeDaoImpl dao = new modeDaoImpl();
                                        log.info("要删除的明细表表名：" + strTbName + ";要删除的数据主键：" + strTbKey);
                                        String strDelResult = dao.delete_Data(strTbName, strTbKey, strKey_m);
                                        String strCode = strDelResult.substring(0, strDelResult.indexOf("|"));
                                        String msg_up = strDelResult.substring(strDelResult.indexOf("|") + 1, strDelResult.length());
                                        if (!"0".equals(strCode)) {
                                            flag = false;
                                            jsonReturn.put("status", "F");
                                            jsonReturn.put("message", msg_up);
                                            break;
                                        }
                                    }
                                }
                            } else {
                                flag = false;
                                jsonReturn.put("status", "F");
                                jsonReturn.put("message", "没有可删除的数据!");
                            }
                        }
                    } else {
                        flag = false;
                        log.info("接口main_list为空！");
                        jsonReturn.put("status", "F");
                        jsonReturn.put("message", "接口传输数据为空!");
                    }
                } else {
                    flag = false;
                    log.info("该表在OA中没有权限,请联系管理员！");
                    jsonReturn.put("status", "F");
                    jsonReturn.put("message", "该表在OA中没有权限,请联系管理员!");
                }


            } else {
                flag = false;
                log.info("接口数据中的建模表没有操作权限,请联系管理员！");
                jsonReturn.put("status", "F");
                jsonReturn.put("message", "接口数据中的建模表没有操作权限,请联系管理员！");
            }
        }catch (Exception ex){
            flag = false;
            ex.printStackTrace();
            log.info("数据传输错误：" + ex.toString());
            jsonReturn.put("status", "F");
            jsonReturn.put("message", "数据传输错误：" + ex.toString());
        }finally {
            if (flag) {
                jsonReturn.put("status", "S");
                jsonReturn.put("message", "操作成功！");
            }
            lock.unlock();
        }
        return "Result is:"+jsonReturn.toString();
    }

}
